package jp.sourceforge.akjrcp.util;


import com.akjava.lib.standards.process.ProcessMonitor;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;

public class FileCopy {
private static int MAX_COPY = 1024*1024*4;
private File srcFile;
private File destFile;
private boolean canceling;
Process process;
public FileCopy(File srcFile,File destFile){
	this.srcFile=srcFile;
	this.destFile=destFile;
}
public boolean isCanceling() {
	return canceling;
}
public void setCanceling(boolean canceling) {
	this.canceling = canceling;
	if(canceling){
		if(process!=null){
			process.destroy();
		}
	}
}

public File getSrcFile() {
	return srcFile;
}
public void setSrcFile(File srcFile) {
	this.srcFile = srcFile;
}
public File getDestFile() {
	return destFile;
}
public void setDestFile(File destFile) {
	this.destFile = destFile;
}

/*
 * warnning 
 * this code clash OS easily when dest file ejected.
 */
public int exec() throws IOException{
	
	String command[]=new String[6];
	command[0]="cmd.exe";
	command[1]="/c";
	command[2]="copy";
	command[3]="/b";
	command[4]=getSrcFile().getAbsolutePath();
	command[5]=getDestFile().getAbsolutePath();
	
	Runtime r=Runtime.getRuntime();
	try {
		//log.info(ArrayUtils.toString(command));
		PrintStreamProcessPrinter s=new PrintStreamProcessPrinter(new PrintWriter(System.out));
		process=r.exec(command);
		ProcessMonitor monitor=new ProcessMonitor(process,s);
		monitor.startMonitor();
		try {
			process.waitFor();
			
			int result=process.exitValue();
			if(result!=0){
				return result;
			}
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	
	if(canceling){
		//canceld;
		return 1;
	}
	return 0;
}

}
